package com.jsy.seckill.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.util.DateUtil;
import com.jsy.common.vo.Result;
import com.jsy.seckill.pojo.SeckillGoods;
import com.jsy.seckill.service.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/seckill/goods")
@CrossOrigin
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;
    /***
     * SeckillGoods分页条件搜索实现
     * @param seckillGoods: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillGoods>> findPage(@RequestBody(required = false) SeckillGoods seckillGoods, @PathVariable int page, @PathVariable int size) {
        //调用SeckillGoodsService实现分页条件查询SeckillGoods
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(seckillGoods, page, size);
        return new Result<PageInfo<SeckillGoods>>("查询成功！", pageInfo);
    }

    /***
     * SeckillGoods分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillGoods>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用SeckillGoodsService实现分页查询SeckillGoods
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(page, size);
        return new Result<PageInfo<SeckillGoods>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param seckillGoods 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search")
    public Result<List<SeckillGoods>> findList(@RequestBody(required = false) SeckillGoods seckillGoods) {
        //调用SeckillGoodsService实现条件查询SeckillGoods
        List<SeckillGoods> list = seckillGoodsService.findList(seckillGoods);
        return new Result<List<SeckillGoods>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用SeckillGoodsService实现根据主键删除
        seckillGoodsService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }


    /***
     * 修改SeckillGoods数据
     * @param seckillGoods 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody SeckillGoods seckillGoods) {
        //调用SeckillGoodsService实现修改SeckillGoods
        seckillGoodsService.update(seckillGoods);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增SeckillGoods数据
     * @param seckillGoods 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody SeckillGoods seckillGoods) {
        //调用SeckillGoodsService实现添加SeckillGoods
        seckillGoodsService.add(seckillGoods);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询SeckillGoods数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<SeckillGoods> findById(@PathVariable Long id) {
        //调用SeckillGoodsService实现根据主键查询SeckillGoods
        SeckillGoods seckillGoods = seckillGoodsService.findById(id);
        return new Result<SeckillGoods>("查询成功！", seckillGoods);
    }

    /***
     * 查询SeckillGoods全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<SeckillGoods>> findAll() {
        //调用SeckillGoodsService实现查询所有SeckillGoods
        List<SeckillGoods> list = seckillGoodsService.findAll();
        return new Result<List<SeckillGoods>>("查询成功！", list);
    }

    /***
     * 获取5个最近的秒杀频道的时间段（根据服务器的地址设置秒杀时间）
     * @return 查询结果
     */
    @GetMapping("/menus")
    public Result<List<Date>> menus() {
        //查看redis中秒杀商品的所有数据
        return new Result<List<Date>>("秒杀频道时间段查询成功！", DateUtil.getDateMenus());
    }


    /***
     *查询时间段内秒杀商品
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result<List<SeckillGoods>> list(String time) {
        //查看redis中秒杀商品的所有数据
        List<SeckillGoods> list =seckillGoodsService.findSeckillGoodsListByTime(time);
        return new Result<List<SeckillGoods>>("查询时间段内秒杀商品成功！", list);
    }

    /***
     *查询时间段内秒杀商品详情页
     * @return 查询结果
     */
    @GetMapping("/one")
    public Result<SeckillGoods> list(String time,Long id) {
        //查看redis中秒杀商品的所有数据
        SeckillGoods seckillGoods=seckillGoodsService.findSeckillGoodsListByTimeAndId(time,id);
        return new Result<SeckillGoods>("查询时间段内秒杀商品成功！", seckillGoods);
    }

}
